package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.LoanWholeProcessCheckedStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.LoanWholeProcessCheckedStatRequest;
import com.tuanzi.loan.vo.response.statistics.LoanWholeProcessCheckedStatResponse;
import org.jooq.SelectField;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanWholeprocessChecked.LOAN_WHOLEPROCESS_CHECKED;

@Repository
public class LoanWholeProcessCheckedStatRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanWholeProcessCheckedStatRepository{
    @Override
    public Page<LoanWholeProcessCheckedStatResponse> list(LoanWholeProcessCheckedStatRequest request, Pageable pageable) {
        List<SelectField<?>> selectFields = new ArrayList<>();
        selectFields.add(LOAN_WHOLEPROCESS_CHECKED.TIME);
        selectFields.add(DSL.sum(LOAN_WHOLEPROCESS_CHECKED.OUTAMOUNT).as("outamount"));
        selectFields.add(DSL.sum(LOAN_WHOLEPROCESS_CHECKED.INNERNUM).as("innernum"));
        selectFields.add(DSL.sum(LOAN_WHOLEPROCESS_CHECKED.OUTNUM).as("outnum"));
        selectFields.add(DSL.sum(DSL.cast(LOAN_WHOLEPROCESS_CHECKED.UV,BigDecimal.class)).as("uv"));

        /*产品id不为全部*/
        if (request.getProductid()==null || !request.getProductid().contains("-2000")){
            selectFields.add(LOAN_WHOLEPROCESS_CHECKED.PRODUCTID);
            /*选择了产品*/
            if (Objects.nonNull(request.getProductid())){
                in(LOAN_WHOLEPROCESS_CHECKED.PRODUCTID,request.getProductid());
            }
            groupBy(LOAN_WHOLEPROCESS_CHECKED.PRODUCTID);
        }

        /*渠道不为全部*/
        if (request.getChannel() ==null || !request.getChannel().contains("9999")){
            selectFields.add(LOAN_WHOLEPROCESS_CHECKED.CHANNEL);
            /*选择了渠道*/
            if (Objects.nonNull(request.getChannel())){
                in(LOAN_WHOLEPROCESS_CHECKED.CHANNEL,request.getChannel());
            }
            groupBy(LOAN_WHOLEPROCESS_CHECKED.CHANNEL);
        }

        /*活动不为全部*/
        if (request.getActivityname()==null || !request.getActivityname().contains("-2000")){
            selectFields.add(LOAN_WHOLEPROCESS_CHECKED.ACTIVITYNAME);
            /*选择了渠道*/
            if (Objects.nonNull(request.getChannel())){
                in(LOAN_WHOLEPROCESS_CHECKED.ACTIVITYNAME,request.getActivityname());
            }
            groupBy(LOAN_WHOLEPROCESS_CHECKED.ACTIVITYNAME);
        }
        SelectField[] selectArray = new SelectField[selectFields.size()];
        selectFields.toArray(selectArray);
        fields(selectArray);
        ge(LOAN_WHOLEPROCESS_CHECKED.TIME,request.getStartDate());
        le(LOAN_WHOLEPROCESS_CHECKED.TIME, request.getEndDate());
        groupBy(LOAN_WHOLEPROCESS_CHECKED.TIME);
        orderBy(LOAN_WHOLEPROCESS_CHECKED.TIME, Sort.Direction.DESC);

        return search(pageable,record -> {
            LoanWholeProcessCheckedStatResponse response = new LoanWholeProcessCheckedStatResponse();
            if (request.getActivityname()!=null && request.getActivityname().contains("-2000")){
                response.setActivityname("全部");
            }else{
                response.setActivityname(record.get(LOAN_WHOLEPROCESS_CHECKED.ACTIVITYNAME));
            }
            if (request.getChannel() !=null && request.getChannel().contains("9999")){
                response.setChannel("全部");
            }else{
                response.setChannel(record.get(LOAN_WHOLEPROCESS_CHECKED.CHANNEL));
            }
            if (request.getProductid()!=null && request.getProductid().contains("-2000")){
                response.setProductid("全部");
            }else{
                response.setProductid(record.get(LOAN_WHOLEPROCESS_CHECKED.PRODUCTID));
            }
            response.setUv(record.get(LOAN_WHOLEPROCESS_CHECKED.field("uv"),BigDecimal.class));
            response.setTime(record.get(LOAN_WHOLEPROCESS_CHECKED.TIME));
            response.setInnernum(record.get(LOAN_WHOLEPROCESS_CHECKED.field("innernum"),Integer.class));
            response.setOutnum(record.get(LOAN_WHOLEPROCESS_CHECKED.field("outnum"),Integer.class));
            response.setOutamount(record.get(LOAN_WHOLEPROCESS_CHECKED.field("outamount",BigDecimal.class)));
            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_WHOLEPROCESS_CHECKED;
    }
}
